import enum
from datetime import date, datetime,time

from sqlalchemy import create_engine,text,Column,Integer,String,Float,DECIMAL,Boolean,Enum,Date,DateTime,Time,Text
from sqlalchemy.orm import declarative_base,sessionmaker

#数据库的对象
HOST = '127.0.0.1'
PORT = 3306
DATABASE = 'itbz'
USER = 'root'
PWD = 'root'
DB_URI = f'mysql+pymysql://{USER}:{PWD}@{HOST}:{PORT}/{DATABASE}'

#创建引擎
engine = create_engine(DB_URI)
Base = declarative_base()
Session = sessionmaker(engine)

class TagEnum(enum.Enum):
    python = 'python'
    flask = 'flask'
    mysql = 'mysql'

class News(Base):
    __tablename__ = 't_news'
    id = Column(Integer,primary_key=True,autoincrement=True)
    price1 = Column(Float)
    price2 = Column(DECIMAL)
    title = Column(String(32))
    is_delete = Column(Boolean)
    tag1 = Column(Enum('python','Flask','Mysql'))
    tag2 = Column(Enum(TagEnum,native_enum=False))
    create_time = Column(Date)
    update_time = Column(DateTime)
    delete_time = Column(Time)
    content = Column(Text)

def add_data():
    new1 = News(price1=1000.00078,
                price2=1000.00078,
                title='测试SQL数据',
                is_delete=True,
                tag1='python',
                tag2=TagEnum.mysql,
                create_time=date(2050,11,11),
                update_time=datetime(2025,11,11,10,11,12),
                delete_time=time(6,7,8),
                content='测试数据')

    with Session() as session:
        session.add(new1)
        session.commit()

if __name__ == '__main__':
    # Base.metadata.drop_all(engine)
    # Base.metadata.create_all(engine)
    add_data()